package com.example.teamwork.impl;

import com.example.teamwork.Shrio.Bean.ActiveUser;
import com.example.teamwork.Shrio.Exception.ServiceException;
import com.example.teamwork.informationGet.LoginLogVO;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.mapper.loginLogMapper;
import com.example.teamwork.pojo.loginLog;
import com.example.teamwork.service.loginLogService;
import com.example.teamwork.utils.IPUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 〈登录日志管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/10/19 14:24
 * @version 1.0
 */

@Service
public class loginLogServiceImpl implements loginLogService {
    @Autowired
    loginLogMapper loginLogMapper;

    /**
     * 登入日志列表
     * @param pageNum
     * @param pageSize
     * @param loginLogVO
     * @return
     */
    @Override
    public PageVO<LoginLogVO> findLoginLogList(Integer pageNum, Integer pageSize, LoginLogVO loginLogVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(loginLog.class);
        Example.Criteria criteria = o.createCriteria();
        o.setOrderByClause("login_time desc");
        if(loginLogVO.getLoginIp()!=null&&!"".equals(loginLogVO.getLoginIp())){
            criteria.andLike("loginIp","%"+loginLogVO.getLoginIp()+"%");
        }
        if(loginLogVO.getLoginUsername()!=null&&!"".equals(loginLogVO.getLoginUsername())){
            criteria.andLike("loginUsername","%"+loginLogVO.getLoginUsername()+"%");
        }
        List<loginLog> loginLogs = loginLogMapper.selectByExample(o);
        List<LoginLogVO> loginLogVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(loginLogs)){
            for (loginLog loginLog : loginLogs) {
                LoginLogVO logVO = new LoginLogVO();
                BeanUtils.copyProperties(loginLog,logVO);
                loginLogVOS.add(logVO);
            }
        }
        PageInfo<loginLog> info=new PageInfo<>(loginLogs);
        return new PageVO<>(info.getTotal(),loginLogVOS);
    }

    @Override
    public void deleteById(Long loginId) {
        loginLogMapper.deleteByPrimaryKey(loginId);
    }


    @Transactional
    @Override
    public void add(HttpServletRequest request) {
        loginLogMapper.insert(createLoginLog(request));
    }
    /**
     * 创建登入日志
     * @param
     * @return
     */
    public static loginLog createLoginLog(HttpServletRequest request) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        loginLog loginLog = new loginLog();
        loginLog.setLoginUsername(activeUser.getUser().getName());
        loginLog.setLoginIp(IPUtils.getIpAddress(request));
        // 获取客户端操作系统
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        OperatingSystem os = userAgent.getOperatingSystem();
        loginLog.setLoginSystem(os.getName());
        loginLog.setLoginBrowser(browser.getName());
        loginLog.setLoginTime(new Date());
        return loginLog;
    }

    /**
     * 批量删除登录日志
     * @param list
     */
    @Override
    public void batchDelete(List<Long> list) {
        for (Long id : list) {
            loginLog loginLog = loginLogMapper.selectByPrimaryKey(id);
            if(loginLog==null){
                throw new ServiceException("id="+id+"登入日志不存在");
            }
            deleteById(id);
        }
    }

    @Override
    public List<loginLog> findAll() {
        return loginLogMapper.selectAll();
    }

}




